﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace DirectorySynchronizer
{
    public class DirSyncOptions
    {
        public static readonly string OptsFileExt = "dso";
        private static readonly string AppDataSubfolderName = string.Empty;
        private static readonly string OptionsFileName = string.Empty;

        public bool PreviewOnly = false;
        public bool DoNotRecurse = false;
        private bool taginsteadofoverwrite = false;
        public bool TagInsteadOfOverwrite
        { //isolated to simplify testing
            get { if (IgnoreOverwriteAndDelete) { return false; } else { return taginsteadofoverwrite; } }
            set { taginsteadofoverwrite = value; }
        }
        private bool taginsteadofdelete = false;
        public bool TagInsteadOfDelete
        { //isolated to simplify testing
            get { if (IgnoreOverwriteAndDelete) { return false; } else { return taginsteadofdelete; } }
            set { taginsteadofdelete = value; }
        }
        public bool IgnoreOverwriteAndDelete = false;
        public bool SuppressDataGridResultsDisplay = false;
        public bool CopyAttributes = false;
        private bool tagcopynotoldfile = false;
        public bool TagCopyNotOldFile
        { //isolated to simplify testing
            get { if (IgnoreOverwriteAndDelete) { return false; } else { return tagcopynotoldfile; } }
            set { tagcopynotoldfile = value; }
        }
        public string SourceDirectory = "";
        public string TargetDirectory = "";
        public string TagString = "tg";

        static DirSyncOptions()
        {
            AppDataSubfolderName = "DirectorySynchronizer";
            OptionsFileName = "DirSync1." + OptsFileExt;
        }
        public DirSyncOptions() { }
        public DirSyncOptions(string argOptionsFileName)
            : this()
        {
            LoadOptionsFile(argOptionsFileName);
        }
        public DirSyncOptions(DirSyncOptions argDirSyncOptions)
        {
            SourceDirectory = argDirSyncOptions.SourceDirectory;
            TargetDirectory = argDirSyncOptions.TargetDirectory;
            TagString = argDirSyncOptions.TagString;
            PreviewOnly = argDirSyncOptions.PreviewOnly;
            DoNotRecurse = argDirSyncOptions.DoNotRecurse;
            TagInsteadOfOverwrite = argDirSyncOptions.TagInsteadOfOverwrite;
            TagInsteadOfDelete = argDirSyncOptions.TagInsteadOfDelete;
            IgnoreOverwriteAndDelete = argDirSyncOptions.IgnoreOverwriteAndDelete;
            SuppressDataGridResultsDisplay = argDirSyncOptions.SuppressDataGridResultsDisplay;
            CopyAttributes = argDirSyncOptions.CopyAttributes;
            TagCopyNotOldFile = argDirSyncOptions.TagCopyNotOldFile;
        }

        public bool SaveOptionSettingsToFile()
        {
            string defaultoptionsfile = GetOptionsFileFullName();
            return SaveOptionSettingsToFile(defaultoptionsfile);
        }
        public bool SaveOptionSettingsToFile(string argFileName)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(this.GetType(), Program.OperantNamespace);
                StreamWriter sw = new StreamWriter(argFileName, false);
                xs.Serialize(sw, this);
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("DirectorySynchronizer SOSTF: " + ex.Message);
                return false;
            }
            return true;
        }

        public bool ExportOptionSettings()
        {
            bool returnresult = false;
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.AddExtension = true;
                sfd.CheckPathExists = true;
                sfd.CreatePrompt = false;
                sfd.DefaultExt = DirSyncOptions.OptsFileExt;
                sfd.FileName = "DirSync1";
                sfd.Filter = "DirSync Options (*." + DirSyncOptions.OptsFileExt + ")|*." + DirSyncOptions.OptsFileExt;
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                sfd.OverwritePrompt = true;
                sfd.ShowHelp = false;
                sfd.SupportMultiDottedExtensions = true;
                sfd.Title = "Directory Synchronizer: Select Options Setup Save File";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    SaveOptionSettingsToFile(sfd.FileName);
                    return returnresult;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DirectorySynchronizer EOS: " + ex.Message);
                returnresult = false;
            }
            return returnresult;
        }

        public static DirSyncOptions ImportOptionsSettings()
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.AddExtension = true;
                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                ofd.DefaultExt = DirSyncOptions.OptsFileExt;
                ofd.FileName = "DirSync1";
                ofd.Filter = "DirSync Options (*." + DirSyncOptions.OptsFileExt + ")|*." + DirSyncOptions.OptsFileExt;
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                ofd.Multiselect = false;
                ofd.ShowHelp = false;
                ofd.SupportMultiDottedExtensions = true;
                ofd.Title = "Directory Synchronizer: Select Options Setup File to Load";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    return LoadOptionsFile(ofd.FileName);
                }
                return null;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Directory Synchronizer IOS:" + ex.Message);
                return null;
            }
        }
        public static DirSyncOptions LoadOptionsFile()
        {
            return LoadOptionsFile(GetOptionsFileFullName());
        }
        public static DirSyncOptions LoadOptionsFile(string argOptionsFileName)
        {
            try
            {
                StreamReader sr = new StreamReader(argOptionsFileName);
                XmlSerializer xs = new XmlSerializer(typeof(DirSyncOptions), Program.OperantNamespace);
                DirSyncOptions kco = (DirSyncOptions)xs.Deserialize(sr);
                sr.Close();
                return kco;
            }
            catch (Exception ex)
            {
                Console.WriteLine("DirectorySynchronizer LOF: " + ex.Message);
                return new DirSyncOptions(); //default to new
            }
        }
        public static string GetLocalAppDataDir()
        {
            string localappdatafolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            return Path.Combine(localappdatafolder, AppDataSubfolderName);
        }
        public static string GetOptionsFileFullName()
        { //creates file with default values if file doesn't exist
            string settingsfolder = GetLocalAppDataDir();
            if (!Directory.Exists(settingsfolder)) { Directory.CreateDirectory(settingsfolder); }
            string settingsfile = Path.Combine(settingsfolder, OptionsFileName);
            if (!File.Exists(settingsfile))
            {
                DirSyncOptions tempdsoptions = new DirSyncOptions();
                tempdsoptions.SaveOptionSettingsToFile(settingsfile);
            }
            return settingsfile;
        }
    }
}
